﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.Model;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.Common.BLL;
using Yipusoft.Exercitation.Model;
using Yipusoft.Exercitation.BLL;

public partial class Combat_SoldierStation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind();
    }

    private void Bind()
    {
        UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        if (user == null)
            return;
        Soldier soldier=new Soldier();
        dltSoldiers.DataSource = soldier.GetSoldiersByWhere(" DepartmentID='" + user.DepartmentID + "' And IsDelete='0'", "SoldierID", "Desc", 500, 0);
        dltSoldiers.DataBind();

        chkStation.DataSource = CodeItem.GetList(" ParentID='SBGW0000'");
        chkStation.DataTextField = "ItemName";
        chkStation.DataValueField = "ItemID";
        chkStation.DataBind();

        
    }

    protected void lnbtnSoldier_Command(object sender, CommandEventArgs e)
    {
        Soldier soldier = new Soldier();
        SoldierArchiveInfo info= soldier.GetSoldierInfoByID(e.CommandArgument.ToString());
        if (info == null)
            return;

        List<SoldierStationInfo> stationList = SoldierStation.GetList(" SoldierID='"+info.SoldierID+"'");
        foreach (ListItem item in chkStation.Items)
        {
            item.Selected = stationList.Where(o => o.StationID == item.Value).Count() > 0;
        }
        lblStation.Text = "正在修改："+info.SoldierName+" 的信息";
        btnSave.CommandArgument = info.SoldierID;
        txtContent.Text = "";
        SoldierCommendInfo cInfo = SoldierCommend.GetSoldierCommend(new Guid(info.SoldierID));
        if (cInfo != null)
        {
            txtContent.Text = cInfo.ContentText;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(btnSave.CommandArgument))
            return;

         Soldier soldier = new Soldier();
         SoldierArchiveInfo info = soldier.GetSoldierInfoByID(btnSave.CommandArgument.ToString());
        if (info == null)
            return;

        SoldierStation.Delete(new Guid(info.SoldierID));
        foreach (ListItem item in chkStation.Items)
        {
            if (item.Selected)
            {
                SoldierStationInfo sInfo = new SoldierStationInfo();
                sInfo.SoldierID = new Guid(info.SoldierID);
                sInfo.StationID = item.Value;
                SoldierStation.Insert(sInfo);
            }
            
        }
        if (!string.IsNullOrEmpty(txtContent.Text.Trim()))
        {
            SoldierCommendInfo cInfo = SoldierCommend.GetSoldierCommend(new Guid(info.SoldierID));
            if (cInfo != null)
            {
                txtContent.Text = cInfo.ContentText;
                SoldierCommend.Update(cInfo);
            }
            else
            {
                cInfo = new SoldierCommendInfo();
                cInfo.SoldierID = new Guid(info.SoldierID);
                cInfo.ContentText = txtContent.Text;
                SoldierCommend.Insert(cInfo);
            }
        }
        Response.Write("<Script>alert('保存成功');;history.back(0);</Script>");
        btnSave.CommandArgument = "";        
        lblStation.Text = "";
        txtContent.Text = "";
    }
}
